using System;
using System.Configuration;

namespace MalRec.Base.ExceptionHandling
{
    public class ExceptionConfigurationConfigHandler : IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler Member

        /// <summary>
        /// Creates a exception configuration
        /// </summary>
        /// <param name="parent">the exception parent</param>
        /// <param name="configContext">the config context</param>
        /// <param name="section">the xml node section</param>
        /// <returns>the exception confirguration</returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            string fileName = String.Empty;
            string dbConnectionString = String.Empty;
            string dbTableName = String.Empty;
            bool useDefaultConnectionString = true;
            string dialogType = String.Empty;

            if (section.SelectSingleNode("FileName") != null
                && !String.IsNullOrEmpty(section.SelectSingleNode("FileName").InnerText))
            {
                fileName = section.SelectSingleNode("FileName").InnerText;
            }

            if (section.SelectSingleNode("DbConnectionString") != null
                && !String.IsNullOrEmpty(section.SelectSingleNode("DbConnectionString").InnerText))
            {
                dbConnectionString = section.SelectSingleNode("DbConnectionString").InnerText;
            }

            if (section.SelectSingleNode("DbTable") != null
                && !String.IsNullOrEmpty(section.SelectSingleNode("DbTable").InnerText))
            {
                dbTableName = section.SelectSingleNode("DbTable").InnerText;
            }

            if (section.SelectSingleNode("DialogType") != null
                && !String.IsNullOrEmpty(section.SelectSingleNode("DialogType").InnerText))
            {
                dialogType = section.SelectSingleNode("DialogType").InnerText;
            }

            if (section.SelectSingleNode("UseDefaultConnectionString") != null
                && !String.IsNullOrEmpty(section.SelectSingleNode("UseDefaultConnectionString").InnerText))
            {
                useDefaultConnectionString = Convert.ToBoolean(section.SelectSingleNode("UseDefaultConnectionString").InnerText);
            }

            return new ExceptionConfiguration(fileName, dbConnectionString, dbTableName, useDefaultConnectionString, dialogType);
        }

        #endregion IConfigurationSectionHandler Member
    }
}